﻿using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;
using System.Text;

namespace Himp.TaskScheduling.Hangfire.Jobs
{
    /// <summary>
    /// 机器人测水_化验结果
    /// </summary>
    public class TBSampleJob : CollectTestDataJob, IHostedService
    {
        #region SQL

        public const string GET_TB_SAMPLE_SQL = @"
            SELECT *
            FROM [dbo].[TB_SAMPLE]
            WHERE [STATUS]='99' and CONVERT(VARCHAR(10), [ENDINGTIME], 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
            ";

        #endregion

        #region 字段

        private readonly ConnectionStringOption _connectionStrings;
        private ILogger<TBSampleJob> _logger;

        #endregion

        public TBSampleJob(ILogger<TBSampleJob> logger
            , IOptions<ConnectionStringOption> connectionStrings) : base(logger, connectionStrings)
        {
            _logger = logger;
            _connectionStrings = connectionStrings.Value;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("--------启动:机器人测水_化验结果--------");

            try
            {
                using (IDbConnection connection = new SqlConnection(_connectionStrings.KaiY_RMAT6580))
                {
                    connection.Open();
                    // 读取数据
                    var samplePreparationRecList = connection.Query<TBSample>(GET_TB_SAMPLE_SQL);
                    var testProjectID = "100";

                    foreach (var item in samplePreparationRecList)
                    {
                        var thirdCode = BottomCodeConvertTestCode(item.BottleNum);

                        if (string.IsNullOrEmpty(thirdCode))
                        {
                            _logger.LogError($"瓶底码{item.BottleNum}:化验码为空");
                            continue;
                        }

                        CollectTestDataModel collectTestDataModel = new CollectTestDataModel();
                        collectTestDataModel.thirdCode = thirdCode;

                        if (collectTestDataModel.thirdCode.Length > 11)
                        {
                            collectTestDataModel.thirdCode = collectTestDataModel.thirdCode.Substring(collectTestDataModel.thirdCode.Length - 11);
                        }

                        collectTestDataModel.gh = item.Operator;
                        collectTestDataModel.peopleName = item.Operator;
                        collectTestDataModel.testTime = item.EndingTime.ToString("yyyy-MM-dd HH:mm:ss");
                        collectTestDataModel.originalRecordNo = $"RMAT6580_{item.ID}_{testProjectID}";
                        collectTestDataModel.labID = "1";
                        collectTestDataModel.testProjectID = testProjectID;
                        collectTestDataModel.testDeviceID = "1";
                        collectTestDataModel.unitID = "0";

                        StringBuilder sbTestZBID = new StringBuilder();
                        StringBuilder sbTestValue = new StringBuilder();
                        StringBuilder sbTestDevice = new StringBuilder();

                        if (item.DeviceNum != 0) //仪器号
                        {
                            sbTestZBID.Append(",100");
                            sbTestValue.Append($",{item.DeviceNum}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.TrayWeight != 0) //称量瓶(盘)质量(g)
                        {
                            sbTestZBID.Append(",102");
                            sbTestValue.Append($",{item.TrayWeight}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.SampleWeight != 0) //试样质量m(g)
                        {
                            sbTestZBID.Append(",103");
                            sbTestValue.Append($",{item.SampleWeight}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.SampleWeight != 0 && item.TrayWeight != 0) //总样质量(g)
                        {
                            sbTestZBID.Append(",104");
                            sbTestValue.Append($",{(item.SampleWeight+ item.TrayWeight)}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.LeftWeight != 0) //烘干后质量(g)
                        {
                            sbTestZBID.Append(",105");
                            sbTestValue.Append($",{item.LeftWeight}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.LeftWeight1 != 0) //一次残重
                        {
                            sbTestZBID.Append(",106");
                            sbTestValue.Append($",{item.LeftWeight1}");
                            sbTestDevice.Append(",10");
                        }
                        if (item.LeftWeight2 != 0) //二次残重
                        {
                            sbTestZBID.Append(",107");
                            sbTestValue.Append($",{item.LeftWeight2}");
                            sbTestDevice.Append(",10");
                        }
                        if (item.LeftWeight3 != 0) //三次残重
                        {
                            sbTestZBID.Append(",108");
                            sbTestValue.Append($",{item.LeftWeight3}");
                            sbTestDevice.Append(",10");
                        }
                        if (item.LeftWeight4 != 0) //四次残重
                        {
                            sbTestZBID.Append(",109");
                            sbTestValue.Append($",{item.LeftWeight4}");
                            sbTestDevice.Append(",10");
                        }
                        if (item.LeftWeight5 != 0) //五次残重
                        {
                            sbTestZBID.Append(",110");
                            sbTestValue.Append($",{item.LeftWeight5}");
                            sbTestDevice.Append(",10");
                        }

                        if (item.ReviseMoisture != 0) //Mt
                        {
                            sbTestZBID.Append(",112");
                            sbTestValue.Append($",{item.ReviseMoisture}");
                            sbTestDevice.Append(",10");
                        }

                        collectTestDataModel.testZBIDs = sbTestZBID.ToString().Length > 0 ? sbTestZBID.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testValues = sbTestValue.ToString().Length > 0 ? sbTestValue.ToString().Substring(1) : string.Empty;
                        collectTestDataModel.testDeviceID = sbTestDevice.ToString().Length > 0 ? sbTestDevice.ToString().Substring(1) : string.Empty;

                        Exec(collectTestDataModel);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(exception: e, "异常:机器人测水_化验结果");
            }

            _logger.LogInformation("--------完成:机器人测水_化验结果--------");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }
}
